/**
 * @name: 年月日 时分秒
 * @param {Number} 时间戳
 * @return {String} yyyy-mm-dd hh:mm:ss
 */
const dateTime = time => {
  let newDate = new Date(time);
  let { y, M, d, h, m, s } = { y: newDate.getFullYear(), M: newDate.getMonth() + 1, d: newDate.getDate(), h: newDate.getHours(), m: newDate.getMinutes(), s: newDate.getSeconds() };
  return `${y}-${addZero(M)}-${addZero(d)}  ${addZero(h)}:${addZero(m)}:${addZero(s)}`;
}

/**
 * @name: 获取当前时间
 * @param {*}
 * @return {*}
 */
const getDateTime = () => {
  let d, s;
  d = new Date();
  let addZero = value => { return value < 10 ? `0${value}` : value };
  s = `${d.getFullYear()}-`;
  s = `${s}${addZero(d.getMonth() + 1)}-`;
  s += `${addZero(d.getDate())} `;
  s += `${addZero(d.getHours())}:`;
  s += `${addZero(d.getMinutes())}:`;
  s += `${addZero(d.getSeconds())}`;
  return s
}

/**
 * @name: 时间补零
 * @param {*}
 * @return {*}
 */
const addZero = obj => {
  return obj < 10 ? `0${obj}` : obj
}

/**
 * @name: 字符替换
 * @param {String} 日期
 * @params {String} 被替换字符
 * @params {String} 替换字符
 * @return {*}
 */
const transFormat = (str, l, r) => {
  let reg = new RegExp(l, 'g') // g表示全部替换，默认替换第一个
  str = str.replace(reg, r)
  return str
}

export default {
  dateTime,
  getDateTime,
  transFormat,
  addZero,
};